package cn.pugle.base.conf;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.*;

/**
 * Created by tzp on 15-10-22.<br>
 * 使用timer定时扫描/ 取值时进行判断
 * 指定时间间隔 ／ 判断文件modifytime
 * refer jinzheng singerv2 ConfigManager
 */
public class RTConfigLoader extends TimerTask {
    Map<FileConfig, Long> configLastModify = new HashMap<>();

    @Override
    public void run() {
        System.out.println("checking whether config change");
        for(Map.Entry<FileConfig,Long> configEntry : configLastModify.entrySet()) {
            FileConfig config = configEntry.getKey();
            long lastModified = config.myConfigFile.lastModified();
            if(lastModified > configEntry.getValue()) {
                System.out.println(config.getClass().getName() + lastModified +" modify,reload");
                config.reLoad();
                configEntry.setValue(lastModified);
            } else {
//                System.out.println(config.getClass().getName() + " not modify");
            }
        }
    }

    public static void main(String[] args) throws Exception{
        String filepath = System.getProperty("user.dir") + "/src/main/resources/RTConfig.properties";
        XXConfig XXConfig = new XXConfig();
        XXConfig.setFile(filepath);
        XXConfig.reLoad();
        RTConfigLoader rtConfigLoader = new RTConfigLoader();//可以用Spring的方式注入configList,和config的文件
        rtConfigLoader.configLastModify.put(XXConfig, 0L);
        new Timer().schedule(rtConfigLoader, 0, 3000L);

//        File f = new File(XXConfig.class.getResource("RTConfig.properties").toURI());
        File f2 = new File(ClassLoader.getSystemResource("RTConfig.properties").toURI());
        System.out.println(f2.lastModified());
    }

    public static abstract class FileConfig {
        File myConfigFile;

        public abstract void reLoad();

        public void setFile(File f) {
            myConfigFile = f;
        }

        public void setFile(String path) {
            myConfigFile = new File(path);
        }

        public Object get(String fieldName) throws NoSuchFieldException, IllegalAccessException {
            Field field = this.getClass().getDeclaredField(fieldName);
            field.setAccessible(true);
            return field.get(this);
        }
    }

    public static class XXConfig extends FileConfig {
        String singerHost;
        List<String> zKHostList;

        @Override
        public void reLoad() {//子类方法只可以抛出比被覆盖方法抛出更低的异常，父类抛出子类异常的父类。
            Properties properties = new Properties();
            FileReader fr = null;
            try {
                fr = new FileReader(myConfigFile);
                properties.load(fr);
                singerHost = (String) properties.get("singerHost");
                zKHostList = Arrays.asList(properties.getProperty("zKHostList").split(","));
                System.out.println("load xxConfig: " + singerHost + " | " + zKHostList);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (NullPointerException e) {
                System.out.println("config file wrong");
            } finally {
                if (fr != null) {
                    try {
                        fr.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }

        }
    }
}
